<template>
  <div>
    <el-form
      ref="ruleForm"
      :model="form"
      :rules="rules"
      size="small"
      label-width="150px"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="合同名称：" prop="contractName">
            <el-input :value="form.contractName" readonly></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="合同编号：" prop="contractCode">
            <el-input :value="form.contractCode" readonly></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="合同金额：" prop="money">
            <el-input type="number" v-model="form.money">
              <el-select v-model="form.units" slot="append" placeholder="请选择" style="width: 130px; padding: 0px 20px; margin: -5px -50px;">
                <el-option
                  v-for="item in unitsList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.label">
                  </el-option>
                </el-select>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="合同完成签订日期：" prop="goodTime">
            <el-date-picker
              v-model="form.goodTime"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择日期"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="合同扫描件：" prop="accessoryList">

            <!-- <Upload
              v-model="form.accessoryList"
              listType="text"
              :limit="10"
            ></Upload> -->
            <Upload v-model="form.accessoryList" fielState="10" :limit="10"></Upload>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="归档审批：" prop="archiveApprovaers">
            <Approver
              :approverList="form.archiveApprovaers"
              @handleChooseapprover="handleChooseapprover"
            ></Approver>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="抄送：" prop="ccPeopleList">
            <Approver
              :isCc="true"
              :approverList="form.ccPeopleList"
              @handleChooseapprover="handleChooseapproverCc"
            ></Approver>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row type="flex" justify="end">
      <el-button @click="onCancel(false)">取消</el-button>
      <el-button type="primary" @click="confirm">确定</el-button>
    </el-row>
  </div>
</template>

<script>
import { Contracts } from '@/api/projectAdministration.js';
// import Upload from '@/views/Projects/components/Upload.vue';
import Approver from '@/views/Projects/components/approver.vue';
export default {
  props: {
    detail: {
      type: Object,
      default: () => { }
    }
  },
  data () {
    return {
      unitsList: [
        {value: '0', label: '元'},
        {value: '1', label: '万元'},
        {value: '2', label: '亿元'}
      ],
      // 是否为修改归档信息
      isUpdata: false,
      form: {
        contractId: '',
        contractName: '',
        contractCode: '',
        money: '',
        goodTime: '',
        accessoryList: [],
        archiveApprovaers: [],
        ccPeopleList: [],
        units: ''
      },
      rules: {
        contractName: [{ required: true, message: '请输入合同名称', trigger: 'blur' }],
        contractCode: [{ required: true, message: '请输入合同编号', trigger: 'blur' }],
        money: [{ required: true, message: '请输入合同金额', trigger: 'blur' }],
        goodTime: [{ required: true, message: '请选择日期', trigger: 'change' }],
        accessoryList: [{ required: true, message: '请上传合同扫描件', trigger: 'change' }],
        archiveApprovaers: [{ required: true, message: '请选择审批人', trigger: 'change' }],
        ccPeopleList: [{ required: true, message: '请选择抄送人', trigger: 'change' }]
      }
    };
  },
  created () {
    this.form.units = this.unitsList[0].label
  },
  components: {
    // Upload,
    Approver
  },
  mounted () {
    console.log('打开弹窗', this.detail);
    if (this.detail) {
      this.isUpdata = this.detail.verifyStatus === 9;
      this.$set(this.form, 'contractId', this.detail.id);
      this.$set(this.form, 'contractName', this.detail.contractName);
      this.$set(this.form, 'contractCode', this.detail.contractCode);
    }
  },
  methods: {
    // 提交
    confirm () {
      console.log('提交', this.form);
      this.$refs['ruleForm'].validate(valid => {
        if (!valid) {
          return false;
        }
        let arr = (this.form.ccPeopleList || []).map(v => {
          return v.userId
        })
        let params = {
          ...this.form,
          ccPeopleList: arr
        };
        let fnName = this.isUpdata ? 'contractUpArchive' : 'contractArchive';
        // 归档提交
        Contracts[fnName](this, params).then(res => {
          console.log('提交返回', res);
          this.$message.success('提交成功');
          this.onCancel(true);
        });
      });
    },
    // 审批选择人员触发
    handleChooseapprover (data) {
      this.$set(this.form, 'archiveApprovaers', data);
    },
    // 抄送选择人员触发
    handleChooseapproverCc (data) {
      // data = data.map(v => v.userId);
      this.$set(this.form, 'ccPeopleList', data);
    },
    // 关闭弹窗
    onCancel (isSuccess, data) {
      if (this.observer != null) {
        this.observer.cancel(isSuccess, data);
      }
    }
  }
};
</script>

<style lang="scss" scoped>
</style>
